package ch.ffhs.ks.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import ch.ffhs.ks.model.Account;
import ch.ffhs.ks.model.Mail;
import ch.ffhs.ks.model.ModelContext;

public class EmailWindow extends JFrame implements ActionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField toField;
	private JTextField ccField;
	private JTextField bccField;
	private JTextField subjectField;
	private JTextArea messageField;
	private JComboBox fromComboBox;

	private JScrollPane messageScrollPane;

	private JButton sendButton;
	private JButton answerButton;
	private JButton redirectButton;

	private Mail mail;

	private DefaultComboBoxModel fromComboBoxModel;

	private ModelContext modelContext;
	
	private ProgressWindow progressWindow;
	
	public EmailWindow(ModelContext modelContext)
	{
		this.modelContext = modelContext;
		this.modelContext.addObserver(this);
		
		setSize(new Dimension(500,400));

		fromComboBoxModel = new DefaultComboBoxModel();

		initGUI();
	}

	public void createNewMail() {
		this.mail = new Mail();
		fromComboBoxModel.removeAllElements();

		for(Account a : this.modelContext.getAccounts())
		{
			fromComboBoxModel.addElement(a);
		}
		
		setControlsEnabled(true);
	}

	public void showMail(Mail m)
	{

		this.mail = m;

		toField.setText(m.getReceiver());
		ccField.setText(m.getCc());
		bccField.setText(m.getBcc());
		subjectField.setText(m.getSubject());
		messageField.setText(m.getContent());
		messageField.setCaretPosition(0);

		fromComboBoxModel.removeAllElements();
		fromComboBoxModel.addElement(m.getSender());

		setControlsEnabled(false);
		sendButton.setEnabled(false);
	}

	public void answerMail(Mail m)
	{
		this.mail = m;

		setControlsEnabled(true);
		sendButton.setEnabled(true);
		answerButton.setEnabled(false);
		redirectButton.setEnabled(false);

		fromComboBoxModel.removeAllElements();

		for(Account a : this.modelContext.getAccounts())
		{
			fromComboBoxModel.addElement(a);
		}

		toField.setText(m.getSender());
		ccField.setText(m.getCc());
		bccField.setText(m.getBcc());
		subjectField.setText("AW: "+m.getSubject());
		messageField.setText("\n========================\n"+m.getContent());
		messageField.setCaretPosition(0);
	}

	public void redirectMail(Mail m)
	{
		this.mail = m;

		setControlsEnabled(true);
		sendButton.setEnabled(true);
		answerButton.setEnabled(false);
		redirectButton.setEnabled(false);

		fromComboBoxModel.removeAllElements();

		for(Account a : this.modelContext.getAccounts())
		{
			fromComboBoxModel.addElement(a);
		}

		subjectField.setText("WG: "+m.getSubject());
		messageField.setText("\n========================\n"+m.getContent());
		messageField.setCaretPosition(0);
	}

	private void setControlsEnabled(boolean enabled)
	{
		toField.setEnabled(enabled);
		ccField.setEnabled(enabled);
		bccField.setEnabled(enabled);
		messageField.setEditable(enabled);
		subjectField.setEnabled(enabled);

		fromComboBox.setEnabled(enabled);
	}

	private void initGUI()
	{

		JPanel masterPanel = new JPanel(new BorderLayout(5,5));
		masterPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		JPanel headPanel = new JPanel(new BorderLayout());

		JPanel buttonsPanel = new JPanel(new FlowLayout());
		sendButton = new JButton("Senden", ImageFactory.createImage("/images/send.png"));
		answerButton = new JButton("Antworten", ImageFactory.createImage("/images/reply.png"));
		redirectButton = new JButton("Weiterleiten", ImageFactory.createImage("/images/forward.png"));

		buttonsPanel.add(sendButton);
		buttonsPanel.add(answerButton);
		buttonsPanel.add(redirectButton);

		sendButton.addActionListener(this);
		answerButton.addActionListener(this);
		redirectButton.addActionListener(this);

		SimpleFormPanel headerPanel = new SimpleFormPanel(45, JLabel.LEFT);

		fromComboBox = headerPanel.addComboBox("Von: ");
		toField = headerPanel.addRow("An:");
		ccField = headerPanel.addRow("Cc:");
		bccField = headerPanel.addRow("Bcc:");
		subjectField = headerPanel.addRow("Betreff:");

		fromComboBox.setModel(fromComboBoxModel);

		headPanel.add(buttonsPanel, BorderLayout.NORTH);
		headPanel.add(headerPanel, BorderLayout.CENTER);

		masterPanel.add(headPanel, BorderLayout.NORTH);

		messageField = new JTextArea();
		messageScrollPane = new JScrollPane(messageField);
		messageField.setEditable(false);

		masterPanel.add(messageScrollPane, BorderLayout.CENTER);
		add(masterPanel);
	
		setVisible(true);
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource().equals(answerButton))
		{
			answerMail(this.mail);
		}else if(e.getSource().equals(redirectButton))
		{
			redirectMail(this.mail);		

		}else if(e.getSource().equals(sendButton))
		{

			Mail newMail = new Mail();
			newMail.setBcc(bccField.getText());
			newMail.setCc(ccField.getText());
			newMail.setContent(messageField.getText());
			newMail.setSubject(subjectField.getText());
			newMail.setReceiver(toField.getText());

			Account account = (Account) fromComboBox.getSelectedItem();

			progressWindow = new ProgressWindow("Das Email wird versendet...");
			progressWindow.setVisible(true);
			
			this.setControlsEnabled(false);

			modelContext.sendEmail(newMail, account);
	
		}

	}

	@Override
	public void update(Observable o, Object arg) {
		progressWindow.setVisible(false);
		setControlsEnabled(true);
	}


}
